﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace _10.UnionAndIntersectOfSets
{
    class NumbersFunctionSet
    {
        private SortedSet<int> members;

        public SortedSet<int> Members { get { return members; } }

        private NumbersFunctionSet()
        {
            members = new SortedSet<int>();
        }

        public NumbersFunctionSet(int a, int b, int c, int minRange = 0, int maxRange = 100000)
        {
            members = new SortedSet<int>();
            int current = a;
            while (current <= maxRange && current >= minRange)
            {
                members.Add(current);
                current = b * current + c;
            }
        }

        public NumbersFunctionSet Union(params NumbersFunctionSet[] otherFunctionsSets)
        {
            NumbersFunctionSet unionSet = new NumbersFunctionSet();
            unionSet.members.UnionWith(this.members);

            foreach (var functionSet in otherFunctionsSets)
            {
                unionSet.members.UnionWith(functionSet.Members);
            }

            return unionSet;
        }

        public NumbersFunctionSet Intersect(params NumbersFunctionSet[] otherFunctionsSets)
        {
            NumbersFunctionSet intersectSet = new NumbersFunctionSet();
            intersectSet.members.UnionWith(this.members);

            foreach (var functionSet in otherFunctionsSets)
            {
                intersectSet.members.IntersectWith(functionSet.Members);
            }

            return intersectSet;
        }
    }
    class UnionAndIntersectOfSets
    {
        static void Main(string[] args)
        {
            int[] a = new int[4];
            int[] b = new int[4];
            int[] c = new int[4];
            NumbersFunctionSet[] f = new NumbersFunctionSet[5];

            string command = String.Empty;

            for (int i = 1; i <= 3; i++)
            {
                string[] input = Console.ReadLine().Split();
                a[i] = int.Parse(input[0]);
                b[i] = int.Parse(input[1]);
                c[i] = int.Parse(input[2]);
                f[i] = new NumbersFunctionSet(a[i], b[i], c[i]);
            }

            command = Console.ReadLine();

            //StreamReader reader = new StreamReader(@"..\..\Tests\test10.in.txt");
            //using (reader)
            //{
            //    for (int i = 1; i <= 3; i++)
            //    {
            //        string[] input = reader.ReadLine().Split();
            //        a[i] = int.Parse(input[0]);
            //        b[i] = int.Parse(input[1]);
            //        c[i] = int.Parse(input[2]);
            //        f[i] = new NumbersFunctionSet(a[i], b[i], c[i]);
            //    }
            //    command = reader.ReadLine();
            //}

            CommandExecution(f, command);

            //StreamWriter writer = new StreamWriter(@"..\..\Tests\test10.out.txt");

            //using (writer)
            //{
            //    foreach (var member in f[4].Members)
            //    {
            //        writer.WriteLine(member);
            //    }
            //}

            foreach (var member in f[4].Members)
            {
                Console.WriteLine(member);
            }
        }

        private static void CommandExecution(NumbersFunctionSet[] f, string command)
        {
            switch (command)
            {
                case "f1+f2": f[4] = f[1].Union(f[2]); break;
                case "f2+f3": f[4] = f[2].Union(f[3]); break;
                case "f1+f3": f[4] = f[1].Union(f[3]); break;

                case "f1*f2": f[4] = f[1].Intersect(f[2]); break;
                case "f1*f3": f[4] = f[1].Intersect(f[3]); break;
                case "f2*f3": f[4] = f[2].Intersect(f[3]); break;

                case "f1+f2+f3": f[4] = f[1].Union(f[2], f[3]); break;
                case "f1*f2*f3": f[4] = f[1].Intersect(f[2], f[3]); break;

                case "f1+f2*f3": f[4] = f[1].Union(f[2]).Intersect(f[3]); break;
                case "f1*f2+f3": f[4] = f[1].Intersect(f[2]).Union(f[3]); break;
            }
        }
    }
}
